exports.bulk_purchase_activity_solitaier_settlement = function (req, res) {

    var bodyParams = req.routeInfo.bodyParams;

    var resultData = {};

    var joinUserData = [];

    //任务流创建
    async.waterfall([

            //查询活动信息
            function (cb) {
                
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_organization_activity_info + "/oa_id/"+bodyParams.oa_id,
                        method: 'POST'
                    },
                    function (err, data) {

                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.activityData = data.data[0];

                        cb();

                    }
                );

            },

            //查询配置信息
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchBulkPurchaseActivityConfig + "/bpac_oaid/"+bodyParams.oa_id,
                        method: 'POST'
                    },
                    function (err, data) {

                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.activityConfigData = data.data[0];

                        cb();

                    }
                );
            },

            //查询所有参与用户
            function(cb){

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.bulkPurchaseActivityJoinInfo + "/bpau_oaid/"+bodyParams.oa_id,
                        method: 'POST'
                    },
                    function (err, data) {

                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }


                        resultData.joinUserData = data.data;

                        cb();

                    }
                );

            },

            //发送私信和短信
            function(cb){

                var tasks = [];

                //循环节点列表
                resultData.joinUserData.forEach(function (item, i) {

                    if(item.openid == "" || item.openid == null){
                        return callback();
                    }

                    var clickurl = config.publicConfig.CLICK_SERVER + "collage_details?oa_id=" + bodyParams.oa_id;

                    R.SEND_HTTP(
                        req,
                        {
                            url: "http://127.0.0.1:3400/weixin/templateMessagesSent",
                            method: 'POST',
                            data  : {
                                openid : item.openid,
                                type   : "pinTuanChengGong",
                                shopname : resultData.activityData.oa_title,
                                clickurl : clickurl
                            }
                        },
                        function (err, data) {

                            //发送短信
                            //console.log("已给" + item.o_name + "发送了私信");
    
                            //发送短信
                            if( item.o_phone == "" ){
                                return callback();
                            }

                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.sendCodeSms,
                                    method: 'POST',
                                    data  : {
                                        phone : item.o_phone,
                                        type  : 3,
                                        activity_name : resultData.activityData.oa_title,
                                        time  : resultData.activityData.oa_end_time
                                    }
                                },
                                function (err, data) {
        
                                    //发送短信
                                    //console.log("已给" + item.o_name + "发送了短信");
        
                                    callback();
        
                                }
                            );

                        }
                    );


                });

                async.waterfall(tasks, cb);

            }

        ], function (err, result) {


            //返回最终拼接结果
            RES.response(res, true);

        }
    );

}